using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

namespace Toenda.Foundation {
	/// <summary>
	/// Settings class for the connection strings
	/// </summary>
	public class ConnectiongString {
		private static Dictionary<string, string> _list = new Dictionary<string, string>();

		/// <summary>
		/// Creates a new instance of <see cref="ConnectiongString"/>
		/// </summary>
		public ConnectiongString() {
			InitList();
		}

		/// <summary>
		/// Initialize the list of connection strings
		/// </summary>
		private static void InitList() {
			lock(_list) {
				if(_list.Count < ConfigurationManager.ConnectionStrings.Count) {
					foreach(ConnectionStringSettings conString in ConfigurationManager.ConnectionStrings) {
						if(!_list.ContainsKey(conString.Name)) {
							_list.Add(conString.Name, conString.ConnectionString);
						}
					}
				}
			}
		}

		/// <summary>
		/// Gets a connections string by its id
		/// </summary>
		/// <param name="connectionStringName"></param>
		/// <returns></returns>
		public static string FromId(string connectionStringName) {
			InitList();
			return _list[connectionStringName];
		}
	}
}
